package app.kotlin_1.view;

import android.content.Context;
import android.os.CountDownTimer;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import app.kotlin_1.R;

/**
 * Created by 刘智强 on 2017/10/24.
 * 闪屏页面
 */

public class SplashView extends FrameLayout {
    private Context context;
    private LayoutInflater layoutInflater;
    private String url;
    private int time = 3000;//单位毫秒 倒计时的总时间
    private int delyTime = 1000;//单位毫秒 时间间隔
    private OnSplashListener onSplashListener;
    private CountDownTimer countDownTimer;//倒计
    private TextView tvTime;
    private SimpleDraweeView sdvSplashView;//背景图

    public int getDelyTime() {
        return delyTime;
    }

    public void setDelyTime(int delyTime) {
        this.delyTime = delyTime;
    }

    public void setOnSplashListener(OnSplashListener onSplashListener) {
        this.onSplashListener = onSplashListener;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
        if (url != null && sdvSplashView != null) {
            sdvSplashView.setImageURI(getUrl());
        }
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

    public SplashView(@NonNull Context context) {
        super(context);
        this.context = context;
        initView();
    }

    public SplashView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();
    }

    public SplashView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }

    /**
     * 初始化view
     */
    private void initView() {
        sdvSplashView = new SimpleDraweeView(context);
        sdvSplashView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        sdvSplashView.setScaleType(ImageView.ScaleType.FIT_XY);
        this.addView(sdvSplashView);
        tvTime = new TextView(context);
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 30, 50, 0);
        params.gravity = Gravity.TOP | Gravity.RIGHT;
        tvTime.setLayoutParams(params);
        tvTime.setTextSize(14);
        tvTime.setPadding(20, 10, 20, 10);
        tvTime.setBackgroundResource(R.drawable.shape_search_edittext);
        this.addView(tvTime);

        sdvSplashView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onSplashListener != null) {
                    onSplashListener.onClickImage();
                }
            }
        });
        tvTime.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onSplashListener != null&&countDownTimer!=null) {
                    countDownTimer.cancel();
                    onSplashListener.onClick();
                }
            }
        });


    }

    /**
     * 开启倒计时
     */
    public void startTimer() {
        countDownTimer = new CountDownTimer(getTime(), getDelyTime()) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvTime.setText("还剩" + millisUntilFinished / 1000 + "秒");
            }

            @Override
            public void onFinish() {
                tvTime.setText("还剩0秒");
                if (onSplashListener != null) {
                    onSplashListener.onTimeFinish();
                }

            }
        };
        countDownTimer.start();

    }

    public interface OnSplashListener {
        void onClick();//点击倒计时

        void onTimeFinish();//倒计时时间完了

        void onClickImage();//点击背景图片
    }
}
